﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlgoImpl
{
    public enum eSideInGraph
    {
        LEFT,
        RIGHT,
        SOURCE,
        DEST
    }

    public class Node
    {
        protected eSideInGraph m_SideInGraph;
        protected readonly string m_Id;
        protected List<Nieghbour> m_Neighbours = new List<Nieghbour>();

        public eSideInGraph Side 
        {
            get { return m_SideInGraph; }
            set { m_SideInGraph = value; } 
        }

        public Node(string id, eSideInGraph side)
        {
            m_Id = id;
            m_SideInGraph = side;
        }

        public void AddNeighbour(string id, int maxWeight)
        {
            m_Neighbours.Add(new Nieghbour(id, maxWeight));
        }

        public void AddNeighbour(Nieghbour neighbour)
        {
            m_Neighbours.Add(neighbour);
        }

        public void DecreaseWeightOfNaighbour(string id)
        {
            Nieghbour nieghbour = getNeighbourById(id);
            if (nieghbour != null)
            {
                nieghbour.DecreaseWeight(); ;
            }
            else
            {
                // throw notfoundexception
            }
        }

        public void IncreaseWeightOfNaighbour(string id)
        {
            Nieghbour nieghbour = getNeighbourById(id);
            if (nieghbour != null)
            {
                nieghbour.IncreaseWeight();
            }
            else
            {
                // throw notfoundexception
            }
        }

        public void SetWeightOfNaighbour(string id, int weight)
        {
            Nieghbour nieghbour = getNeighbourById(id);
            if (nieghbour != null)
            {
                nieghbour.Weight = weight;
            }
            else
            {
                // throw notfoundexception
            }
        }

        public void SetWeightOfAllNaighbours(int weight)
        {
            foreach (Nieghbour nei in m_Neighbours)
            {
                nei.Weight = weight;
            }
        }

        public bool IsHaveNeighbours()
        {
            return m_Neighbours.Count > 0;
        }

        public bool IsHaveNeighboursWithPositiveWeight()
        {
            foreach (Nieghbour nei in m_Neighbours)
            {
                if (nei.Weight > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public bool IsHaveNeighboursWithWeight(int weight)
        {
            foreach (Nieghbour nei in m_Neighbours)
            {
                if (nei.Weight == weight)
                {
                    return true;
                }
            }
            return false;
        }

        public Nieghbour getNeighbourById(string id)
        {
            foreach (Nieghbour neighbour in m_Neighbours)
            {
                if (neighbour.Id == id)
                {
                    return neighbour;
                }
            }
            return null;
        }

        public List<Nieghbour> Neighbours
        {
            get { return m_Neighbours; }
        }

        public string Id
        {
            get { return m_Id; }
        }

        public override string ToString()
        {
            string toReturn = string.Empty;
            if (IsHaveNeighbours())
            {
                toReturn = string.Format("Id = {0} --> Nieghbours: ", m_Id);
                foreach (Nieghbour nei in m_Neighbours)
                {
                    toReturn += nei.ToString();
                }
            }
            else
            {
                toReturn = string.Format("Id = {0}", m_Id);
            }
            return toReturn;
        }
    }
}
